#!/usr/bin/env bash
set -eu

ROOT_DIR=$(git rev-parse --show-toplevel)

patch_package() {
    node_modules/.bin/patch-package
}

# Since we take react-native from Git, some packages that ship with it
# and are used in the build need their build scripts run.
build_rn_packages() {
    (
        cd node_modules/react-native/packages/react-native-codegen/
        # A copy of the package's `yarn build` script
        # with a `--verbose` flag removed.
        yarn clean && node scripts/build.js
    )
}

pod_install() {
    [[ "$OSTYPE" == "darwin"* ]] \
        || return 0

    if ! hash pod 2>/dev/null; then
        cat >&2 <<EOF
The Zulip mobile app requires CocoaPods for its iOS build. Please
install it, following the instructions at
https://guides.cocoapods.org/using/getting-started.html.

Then, rerun yarn.
EOF
        exit 1
    fi

    pod install --project-directory="$ROOT_DIR/ios"
}

jetify() {
    node_modules/.bin/jetify
}

patch_package

build_rn_packages

pod_install

jetify

echo "tools/postinstall finished with no errors!"
